// pages/home/order.js
const app = new getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    is_prohibit:false,
    address: [],
    is_address: false,
    address_id: 0,
    orderDeatils: [],
    box_arr:[],
    box_num:[],
    box_index:0,
    brand_arr: [],
    brand_num: [],
    brand_index: 0,
    select_arr:[],
    select_num: [],
    select_index:0,
    select_name:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.setData({
      id: options.id
    });
    if (options.prohibit){
      this.setData({
        is_prohibit: true
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 获取默认地址
    if (!this.data.is_prohibit){
      this.getDefaultAddress(this.data.address_id);
    }
    // 获取订单信息
    this.getOrderInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 跳转到收货地址
  onTapAddress: function () {
    wx.navigateTo({
      url: '/pages/me/receivingAddress?order=1',
    })
  },
  // 获取默认地址
  getDefaultAddress: function (address_id) {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getDefaultAddress',
      method: 'GET',
      data: {
        openid: app.globalData.openid,
        address_id: address_id
      },
      success: function (e) {
        if (e.data) {
          that.setData({
            address: e.data,
            address_id: e.data.id,
            is_address: true
          });
        }else{
          that.setData({
            is_address: false,
            address_id:0
          });
        }
        // 修改订单的收货地址id
        that.changeAddress();
      }
    })
  },
  // 修改订单的收货地址
  changeAddress:function(){
    wx.request({
      url: app.globalData.serverUrl + 'changeAddress',
      method: 'GET',
      data: {
        id: this.data.orderDeatils.id,
        openid: app.globalData.openid,
        address_id: this.data.address_id
      },
      success: function (e) {
        console.log(e);
      }
    })
  },
  // 获取订单信息
  getOrderInfo: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getOrderInfo',
      method:'GET',
      data:{
        order_id:this.data.id
      },
      success:function(e){
        console.log(e);
        that.setData({
          orderDeatils:e.data
        });
        // 获取所有的盒子
        that.getBoxList();
        // 获取当前盒子的所有价格
        that.getBoxPrice();
        // 获取当前盒子的所有品牌
        that.getBoxbrand();
        // 如果是重永久订单进来的,获取收货地址
        if(that.data.is_prohibit){
          that.getDefaultAddress(that.data.orderDeatils.address_id);
        }
      }
    })
  },
  // 获取所有的盒子
  getBoxList:function(){
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxList',
      method:'GET',
      success:function(e){
        // console.log(e);
        that.setData({
          box_arr: e.data
        });
        that.setBoxValue();
      }
    })
  },
  // 设置picker选择盒子的值
  setBoxValue: function () {
    var arr = [];
    for (var x in this.data.box_arr) {
      arr.push(this.data.box_arr[x]['name']);
      if (this.data.box_arr[x]['name'] == this.data.orderDeatils.box_name) {
        // var box_name = 'orderDeatils.box_name';
        this.setData({
          box_index: x
          // [box_name]: this.data.box_arr[x]['name']
        });
      }
    }
    this.setData({
      box_num: arr
    });
  },
  // 修改盒子
  bindBoxChange: function (e) {
    // console.log(e);
    for (var x in this.data.box_arr) {
      if (x = e.detail.value) {
        var box_id = 'orderDeatils.box_id';
        var box_name = 'orderDeatils.box_name';
        var cover_img = 'orderDeatils.cover_img';
        this.setData({
          [box_id]:this.data.box_arr[x]['id'],
          [box_name]: this.data.box_arr[x]['name'],
          [cover_img]: this.data.box_arr[x]['cover_img'],
          box_index: x,
        });
        break;
      }
    }
    console.log(this.data.orderDeatils);
    // 修改订单信息
    wx.request({
      url: app.globalData.serverUrl + 'changeOrder',
      method:'GET',
      data:{
        id: this.data.orderDeatils.id,
        box_id:this.data.orderDeatils.box_id,
        sum_box: this.data.orderDeatils.sum_box,
      },
      success:function(e){
        console.log(e);
      }
    })
    this.getBoxPrice();
    this.getBoxbrand(1);
  },
  // 获取该盒子的所有品牌
  getBoxbrand: function (isBoxChange) {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'getBoxAllbrand',
      method: 'GET',
      data:{
        boxId: this.data.orderDeatils.box_id
      },
      success: function (e) {
        // console.log(e);
        that.setData({
          brand_arr: e.data
        });
        that.setBrandValue(isBoxChange);
      }
    })
  },
  // 设置picker选择盒子的值
  setBrandValue: function (isBoxChange) {
    var arr = [];
    for (var x in this.data.brand_arr) {
      arr.push(this.data.brand_arr[x]['brand']);
      if (this.data.brand_arr[x]['brand'] == this.data.orderDeatils.brand) {
        // var brand = 'orderDeatils.brand';
        this.setData({
          brand_index: x
          // [brand]: this.data.brand_arr[x]['brand']
        });
      }
    }
    this.setData({
      brand_num: arr
    });
    if (isBoxChange == 1) {
      var brand = 'orderDeatils.brand';
      this.setData({
        brand_index: 0,
        [brand]: this.data.brand_arr[0]['brand']
      });
    }
  },
  // 修改盒子的品牌
  bindBrandChange: function (e) {
    // console.log(e);
    for (var x in this.data.brand_arr) {
      if (x = e.detail.value) {
        var brand = 'orderDeatils.brand';
        this.setData({
          [brand]: this.data.brand_arr[x]['brand'],
          brand_index: x,
        });
        break;
      }
    }
    // 修改订单的盒子品牌
    wx.request({
      url: app.globalData.serverUrl + 'changeBrand',
      method:'GET',
      data:{
        id: this.data.orderDeatils.id,
        brand:this.data.orderDeatils.brand
      },
      success:function(e){
        console.log(e);
      }
    })
    console.log(this.data.orderDeatils);
  },
  // 获取盒子所有的价位
  getBoxPrice:function(){
    var that = this;
    wx.request({
      url:app.globalData.serverUrl + 'getBoxPrice',
      method:'GET',
      data:{
        boxId: this.data.orderDeatils.box_id
      },
      success:function(e){
        // console.log(e);
        that.setData({
          select_arr:e.data
        });
        // 设置一下选择时间picker的值
        that.setSelectValue();
      }
    })
  },
  // 设置picker选择月份的值
  setSelectValue:function(){
    var arr = [];
    for (var x in this.data.select_arr) {
      //x = index
      // that.data.imgs.push({ path: temp[tm], label: '添加标签' });
      // console.log(this.data.select_arr[x]['month'], this.data.select_arr[x]['name']);
      arr.push(this.data.select_arr[x]['name']);
      if (this.data.select_arr[x]['month'] == this.data.orderDeatils.sum_box){
        var price = 'orderDeatils.sum_price';
        this.setData({
          [price]: this.data.select_arr[x]['price'],
          select_index: x,
          select_name: this.data.select_arr[x]['name']
        });
      }
    }
    this.setData({
      select_num:arr
    });
  },
  // 修改月份
  bindMonthChange:function(e){
    // console.log(e);
    for(var x in this.data.select_arr){
      if (x = e.detail.value){
        var price = 'orderDeatils.sum_price';
        var sum_box = 'orderDeatils.sum_box';
        var rest_box = 'orderDeatils.rest_box';
        this.setData({
          [price]: this.data.select_arr[x]['price'],
          [sum_box]: this.data.select_arr[x]['month'],
          [rest_box]: this.data.select_arr[x]['month'],
          select_index: x,
          select_name: this.data.select_arr[x]['name']
        });
        break;
      }
    }
    wx.request({
      url: app.globalData.serverUrl + 'changePrice',
      method:'GET',
      data:{
        id: this.data.orderDeatils.id,
        sum_box: this.data.orderDeatils.sum_box,
      },
      success:function(e){
        console.log(e);
      }
    })
    console.log(this.data.orderDeatils);
  },
  // 支付按钮
  btnpay:function(){
    // var that = this;
    // 判断一下是否有收获地址
    if (this.data.is_address){
      this.wxpay();
    }else{
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '请添加收货地址！',
        confirmColor: '#fc5871'
      })
    }
  },
  // 获取微信支付参数,挑起微信支付
  wxpay:function(){
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'wxpay',
      method: 'GET',
      data: {
        openid: app.globalData.openid,
        id: this.data.orderDeatils.id
      },
      success: function (e) {
        // console.log(e.data.appId);
        if (e.data.package) {
          wx.requestPayment({
            timeStamp: e.data.timeStamp,
            nonceStr: e.data.nonceStr,
            package: e.data.package,
            signType: 'MD5',
            paySign: e.data.paySign,
            success(res) {
              // console.log(res);
              if(res.errMsg == 'requestPayment:ok'){
                that.isPeriod();
              }else{
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '支付失败！',
                  confirmColor: '#fc5871'
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: '支付失败！',
            confirmColor: '#fc5871'
          })
        }
      }
    })
  },
  // 判断是否设置经期
  isPeriod: function () {
    var that = this;
    wx.request({
      url: app.globalData.serverUrl + 'isperiod',
      method: 'GET',
      data: {
        openid: app.globalData.openid
      },
      success: function (e) {
        //  console.log(e);
        if (e.data.status == 0) {
          // 设置经期
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 800,
            success: function (res) {
              setTimeout(function () {
                wx.navigateTo({
                  url: '/pages/me/periodsetting?fisrt=1'
                })
              }, 800);
            }
          })
        } else {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 800,
            success: function (res) {
              setTimeout(function () {
                wx.switchTab({
                  url: '/pages/home/index'
                })
              }, 800);
            }
          })
        }
      }
    })
  },
  post:function(){
    wx.request({
      url: 'https://www.ai-quick.com/admin.php/api/notify',
      method:'POST',
      success:function(e){
        console.log(e);
        console.log(e.data.appid);
      }
    })
  }
})